/**
 * 
 */
package kr.pe.silent.etweeter.dialogs;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import kr.pe.silent.etweeter.ETweeter;

/**
 * @author Sanghoon Lee
 * $Id$
 * 
 */
public class OAuthDialog extends Dialog {

	private static final Point DIALOG_SIZE = new Point(920, 550);
	
	private Browser mBrowser;
	private Text mText;
	private Button mButton;

	private Twitter twitter;

	private RequestToken requestToken;
	
	/**
	 * @param parentShell
	 */
	public OAuthDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * @param parentShell
	 */
	public OAuthDialog(IShellProvider parentShell) {
		super(parentShell);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		
		newShell.setText("eTweeter authorization");
	}

	@Override
	protected Point getInitialSize() {
		return DIALOG_SIZE;
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite composite = (Composite) super.createContents(parent);
		
//		getButton(IDialogConstants.OK_ID).setEnabled(false);
		
		return composite;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite body = (Composite) super.createDialogArea(parent);
		GridLayout layout = (GridLayout) body.getLayout();
		layout.numColumns = 2;
		layout.makeColumnsEqualWidth = false;
		
		GridData gd;
		
		mBrowser = new Browser(body, SWT.BORDER);
		gd = new GridData(GridData.FILL_BOTH);
		gd.horizontalSpan = 2;
		mBrowser.setLayoutData(gd);
		
		Label mLabel = new Label(body, SWT.None);
		mLabel.setText("Login and enter the PIN:");
		gd = new GridData(GridData.BEGINNING);
		mLabel.setLayoutData(gd);
		
		mText = new Text(body, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		mText.setLayoutData(gd);
		
		parent.getDisplay().asyncExec(new Runnable() {
			@Override
			public void run() {
				loadPage();
			}
		});
		
		addListener();
		
		return body;
	}

	private void loadPage() {
		twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(ETweeter.OAuthConsumerKey, ETweeter.OAuthConsumerSecret);
		try {
			requestToken = twitter.getOAuthRequestToken();
			mBrowser.setUrl(requestToken.getAuthorizationURL());
		} catch (TwitterException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void okPressed() {
		String pin = mText.getText();
		AccessToken accessToken = null;
		
		try {
			accessToken = twitter.getOAuthAccessToken(requestToken, pin);
			System.out.println(twitter.verifyCredentials().getId());
		} catch (TwitterException te) {
			if (401 == te.getStatusCode()) {
				System.out.println("Unable to get the access token.");
			} else {
				te.printStackTrace();
			}
		}
		
		if (accessToken != null) {
			super.okPressed();
		}
		
		// TODO Sanghoon. support correct reaction for pin.
		
		
		
		
	}
	
	private void addListener() {
		mText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				
			}
		});
	}

}
